﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace vmMachine
{
    public partial class Form1 : Form,Observer
    {
        VendingMachineLogic machine;
        MoneyDisplay disp;
        StateUML stateMachine;
        Boolean pause = false;
        int speed = 0;

        public void update(int i)
        {
            switch (i)
            {
                case 1: 
                    //drink selected
                    stateMachine.drinkSelected();
                    break;
               
                case 2:
                    //change button clicked
                    stateMachine.changeButtonClicked();
                    break;
                
                case 3:
                    //coin inserted
                    stateMachine.coinInserted();
                    break;
            }
            disp.updateDisplay();
        }

        public Form1()
        {
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            this.label3.Text = speed.ToString();
            this.machine = new VendingMachineLogic(this);
            this.disp = new MoneyDisplay(machine, this);
            stateMachine = new StateUML(this,machine);

        }

        //getter&setter for the 'Money' label
        public Label Display
        {
            set
            {
                this.display.Text = value.ToString();
            }
            get
            {
                return this.display;
            }
        }
        
        //getter&setter for the 'change' label
        public Label ChangeCounter
        {
            set
            {
                this.changeCounter.Text = value.ToString();
            }
            get
            {
                return this.changeCounter;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void changeButton_Click(object sender, EventArgs e)
        {
            //change button was clicked
            machine.changeButtonClicked();
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //coca cola = 1;
            machine.drinkSelected(1);


        }

        private void button10_Click(object sender, EventArgs e)
        {
            //neviot = water = 2
            machine.drinkSelected(2);

        }

        private void button9_Click(object sender, EventArgs e)
        {
            //prigat = 3
            machine.drinkSelected(3);

        }

        private void button12_Click(object sender, EventArgs e)
        {
            //pepsi = 4
            machine.drinkSelected(4);

        }

        private void button11_Click(object sender, EventArgs e)
        {
            //anavim = 5
            machine.drinkSelected(5);

        }

        private void button8_Click(object sender, EventArgs e)
        {
            //anavim = 6
            machine.drinkSelected(6);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1 shekel
            machine.coinInserted(1);


        }

        private void oneShekel_Click(object sender, EventArgs e)
        {
            //2 shekel
            machine.coinInserted(2);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //5 shekel
            machine.coinInserted(5);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //10 shekel
            machine.coinInserted(10);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //0.5 shekel
            machine.coinInserted(0.5);

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //0.1 shekel
            machine.coinInserted(0.1);

        }

        private void speedPlus_Click(object sender, EventArgs e)
        {
            if (stateMachine.speed > 250)
                stateMachine.speed = (int)(stateMachine.speed - 250);
            else
                stateMachine.speed = 250;

            speed++;
            this.label3.Text = speed.ToString();
        }

        private void Pause_Click(object sender, EventArgs e)
        {

            if (!pause)
            {
                this.speedMinus.Enabled = false;
                this.speedPlus.Enabled = false;

                stateMachine.pauseTimers();
                disp.pause();
                pause = true;
                Pause.Text = "Resume";
            }
            else
            {
                stateMachine.startTimers();
                disp.resume();
                pause = false;
                Pause.Text = "Pause";
                this.speedMinus.Enabled = true;
                this.speedPlus.Enabled = true;
            }
        }

        private void speedMinus_Click(object sender, EventArgs e)
        {
            stateMachine.speed = (int)(stateMachine.speed +250);
            speed--;
            this.label3.Text = speed.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}
